//
//  NSRange+Ex.swift
//  GhostWorld
//
//  Created by Kam on 2018/8/2.
//  Copyright © 2018 LKGood. All rights reserved.
//

import Foundation

extension NSRange {
  private init(string: String, lowerBound: String.Index, upperBound: String.Index) {
    let utf = string.utf16
    
    let lowerBound = lowerBound.samePosition(in: utf)
    let location = utf.distance(from: utf.startIndex, to: lowerBound!)
    let length = utf.distance(from: lowerBound!, to: upperBound.samePosition(in: utf)!)
    
    self.init(location: location, length: length)
  }
  
  public init(range: Range<String.Index>, in string: String) {
    self.init(string: string, lowerBound: range.lowerBound, upperBound: range.upperBound)
  }
  
  public init(range: ClosedRange<String.Index>, in string: String) {
    self.init(string: string, lowerBound: range.lowerBound, upperBound: range.upperBound)
  }
}
